/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-9         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.coocaa.x.uipackage.widget.WaitingBar;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.ColorButton;
import com.tianci.appstore.utils.SkyConstants;

import java.util.List;

public class ConfirmProcessDialog extends CommonAppOpDialog {

    private TextView mResultText = null;
    private WaitingBar mWaitingBar = null;
    private TextView mFailedContent = null;
    private ColorButton mConfirmBtn = null;
    private ImageView mDivide = null;
    private TextView mContent = null;
    private FrameLayout mTipsLayout = null;
    private Bitmap mWaitBitmap = null;

    public ConfirmProcessDialog(Context context) {
        super(context);
    }

//    @Override
//    public void setData(String appId, String title, String content, String iconUrl)
//    {
//        if (mResultText != null)
//        {
//            mainLayout.removeView(mResultText);
//            mainLayout.removeView(mWaitingBar);
//            mainLayout.addView(contentText);
//            mainLayout.addView(btnLayout);
//        }
//        super.setData(appId, title, content, iconUrl);
//    }

    public void setIconTitle(Drawable bgDrawable, String title) {
        iconImage.setBackgroundDrawable(bgDrawable);
        iconImage.setImageDrawable(null);
        mTitleView.setText(title);
    }

    public void addLittleIcon(Drawable littleIcon) {
        iconImage.setPadding(SkyConstants.iconWidth / 2, SkyConstants.iconWidth / 2, 0, 0);
        iconImage.setImageDrawable(littleIcon);
    }

    public void setContent(String content) {
        contentText.setText(content);
    }

    public void initStatus() {
        mainLayout.removeAllViews();
        mainLayout.addView(contentText);
        mainLayout.addView(btnLayout);
    }

    public void enterWaitState(String content) {
        contentText.setText(content);
        setResponseBackKey(false);
        if (mWaitingBar == null) {
            mWaitingBar = new WaitingBar(mContext);
            SkinManager.setBackground(mWaitingBar, R.drawable.dialog_waiting_bg);
            mWaitingBar.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SkyConstants.Div(870), SkyConstants.Div(9));
            mWaitingBar.setLayoutParams(params);
            mWaitingBar.setSpeed(10);
            mWaitBitmap = SkinManager.getResourceBitmap(R.drawable.dialog_waiting_front);
            mWaitingBar.setWaitingBitmap(mWaitBitmap, SkyConstants.Div(60));
        }
        mainLayout.removeView(btnLayout);
        mainLayout.addView(mWaitingBar);
        mWaitingBar.startAnim();
    }

    public void onDestory() {
        if (mWaitBitmap != null && mWaitBitmap.isRecycled()) {
            mWaitBitmap.recycle();
            mWaitBitmap = null;
        }
    }

    public void enterResultState(String content) {
        if (mWaitingBar != null)
            mWaitingBar.stopAnim();
        mainLayout.removeAllViews();
        if (mResultText == null) {
            mResultText = UICommon.newTextView(mContext, SkyConstants.Dpi(38), R.color.defaultColor);
            mResultText.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            mResultText.setPadding(SkyConstants.Div(9), SkyConstants.Div(69), 0, SkyConstants.Div(63));
            mResultText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.do_complete, 0, 0, 0);
            mResultText.setGravity(Gravity.CENTER_VERTICAL);
            mResultText.setSingleLine(false);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            mResultText.setLayoutParams(params);
        }
        mainLayout.addView(mResultText);
        mResultText.setText(content);
        setResponseBackKey(true);
    }

    public void enterFailedState(List<SkyListAppData> appDatas, String tip) {
        mainLayout.removeAllViews();
        if (mTipsLayout == null) {
            mTipsLayout = new FrameLayout(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SkyConstants.Div(870), LayoutParams.MATCH_PARENT);
            mTipsLayout.setLayoutParams(params);
            addFailedContent(mTipsLayout);
            addConfirmButton(mTipsLayout);
        }
        mainLayout.addView(mTipsLayout);
        addImageDivide();
        addContentView();
        StringBuilder builder = new StringBuilder();
        for (SkyListAppData data : appDatas) {
            builder.append(data.getTitle()).append("\n");
        }
        mFailedContent.setText(tip);
        mContent.setText(builder.toString());
        setResponseBackKey(true);
        mConfirmBtn.requestFocus();
    }

    private void addFailedContent(FrameLayout layout) {
        if (mFailedContent == null) {
            mFailedContent = UICommon.newTextView(mContext, SkyConstants.Dpi(38), R.color.defaultColor);
            mFailedContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            mFailedContent.setGravity(Gravity.CENTER_VERTICAL);
            mFailedContent.setCompoundDrawablesWithIntrinsicBounds(R.drawable.problem, 0, 0, 0);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
            mFailedContent.setLayoutParams(params);
            layout.addView(mFailedContent);
        }
    }

    private void addConfirmButton(FrameLayout layout) {
        if (mConfirmBtn == null) {
            mConfirmBtn = new ColorButton(mContext, R.string.confirm, R.drawable.btn_green, R.drawable.btn_green_focus);
            mConfirmBtn.setTag(10);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(UICommon.btnWidth, UICommon.btnHeight, Gravity.RIGHT);
            mConfirmBtn.setOnClickListener(this);
            mConfirmBtn.setLayoutParams(params);
            layout.addView(mConfirmBtn);
        }

    }

    private void addImageDivide() {
        if (mDivide == null) {
            mDivide = new ImageView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, SkyConstants.Div(30), 0, 0);
            mDivide.setLayoutParams(params);
            SkinManager.setBackground(mDivide, R.drawable.pic_line);
        }
        mainLayout.addView(mDivide);
    }


    private void addContentView() {
        if (mContent == null) {
            mContent = new TextView(mContext);
            mContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            mContent.setTextSize(SkyConstants.Dpi(38));
            mContent.setTextColor(Color.WHITE);
            mContent.setLineSpacing(5, 1.2f);
            //mContent.setBackgroundColor(Color.BLUE);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, SkyConstants.Div(165));
            params.setMargins(0, SkyConstants.Div(15), 0, 0);
            mContent.setLayoutParams(params);
            mContent.setScrollbarFadingEnabled(true);
            mContent.setFocusable(true);
            mContent.setFocusableInTouchMode(true);
            mContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        }
        mainLayout.addView(mContent);
    }

    public void setBtnFocusTag(int tag) {
        if (btnLayout.findViewWithTag(tag) != null) {
            btnLayout.findViewWithTag(tag).requestFocus();
        }
    }

    @Override
    public void onClick(View v) {
        if ((Integer) (v.getTag()) == 10) {
            this.dismiss();
            return;
        }
        super.onClick(v);
    }
}
